package MyFragment;

import android.app.usage.UsageStats;
import android.app.usage.UsageStatsManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.SystemClock;
import android.provider.Settings;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.qb.devtest.MainActivity;
import com.qb.devtest.MyApplication;
import com.qb.devtest.R;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import utils.DevInfoUtil;
import utils.LoadingUtil;
import utils.MyUtil;
import utils.SystemPropertiesReflection;

public class FragmentDevice extends Fragment {
    public TextView tvInfo,tvCpu,tvModel,tvSn,tvRam,tvRom,tvScreen1,tvScreen2;
    public TextView tvAndroid,tvVersion,tvSysdate,tvKjsc,tvKjcs,tvLastUpdate;
    Context ctx = MyApplication.getInstance();
    MainActivity activity= (MainActivity) getActivity();
    SharedPreferences SP = ctx.getSharedPreferences("dev_test_setting", ctx.MODE_PRIVATE);
    // 轮询任务
    private Timer mTimer;
    private TimerTask mTimerTask;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_device, container, false);
        tvInfo = view.findViewById(R.id.tv_device_info);
        tvCpu = view.findViewById(R.id.tv_cpu);
        tvModel = view.findViewById(R.id.tv_model);
        tvSn = view.findViewById(R.id.tv_sn);
        tvRam = view.findViewById(R.id.tv_ram);
        tvRom = view.findViewById(R.id.tv_rom);
        tvScreen1 = view.findViewById(R.id.tv_screen1);
        tvScreen2 = view.findViewById(R.id.tv_screen2);

        tvAndroid = view.findViewById(R.id.tv_android);
        tvVersion = view.findViewById(R.id.tv_version);
        tvSysdate = view.findViewById(R.id.tv_sys_time);
        tvKjsc = view.findViewById(R.id.tv_kjsc);
        tvKjcs = view.findViewById(R.id.tv_kjcs);
        tvLastUpdate = view.findViewById(R.id.tv_last_update);

        Button btnCopy = view.findViewById(R.id.btn_copy);
        btnCopy.setOnClickListener(view1 -> {
            String s = tvCpu.getText()+"\n"+tvModel.getText()+"\n"+tvSn.getText()+"\n"
                    +tvRam.getText()+"\n"+tvRom.getText()+"\n"+tvScreen1.getText()+"\n"
                    +tvScreen2.getText()+"\n"+tvAndroid.getText()+"\n"+tvVersion.getText()+"\n"
                    +tvSysdate.getText()+"\n"+tvKjsc.getText()+"\n"+tvKjcs.getText()+"\n"+tvLastUpdate.getText();
            MyUtil.copyStr(ctx, s);
        });

        loadData();
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        // 在这里初始化你的Fragment视图组件
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        stopTask(mTimer);
    }

    public void loadData() {
        /*String a1 = "getCpuInfo:"+DevInfoUtil.getCpuInfo("");
        String a2 = "getBrand:"+DevInfoUtil.getBrand();
        String a3 = "getOsInfo:"+DevInfoUtil.getOsInfo();
        String a4 = "getCpuModel:"+DevInfoUtil.getCpuModel();
        String a5 = "getCpuName:"+DevInfoUtil.getCpuName();
        String a6 = "getCurCpuFreq:"+DevInfoUtil.getCurCpuFreq();
        String a7 = "getFingerPrint:"+DevInfoUtil.getFingerPrint();
        String a8 = "getMaxCpuFreq:"+DevInfoUtil.getMaxCpuFreq();
        String a9 = "getMinCpuFreq:"+DevInfoUtil.getMinCpuFreq();
        String a10 = "getModel:"+DevInfoUtil.getModel();
        String a11 = "getSN:"+DevInfoUtil.getSN();
        String a12 = "getWlanMac:"+DevInfoUtil.getWlanMac(ctx);
        String a13 = "getBleInfo:"+DevInfoUtil.getBleInfo(ctx).toString();
        String a14 = "getLocalIP:"+DevInfoUtil.getLocalIP().toString();
        String a15 = "getNetworkType:"+DevInfoUtil.getNetworkType(ctx).toString();
        String a16 = "getRAM:"+DevInfoUtil.getRAM(ctx).toString();
        String a17 = "getROM:"+DevInfoUtil.getROM(ctx).toString();
        String a18 = "getScreenInfo:"+DevInfoUtil.getScreenInfo(ctx).toString();
        String a19 = "SystemPropertiesReflection:"+SystemPropertiesReflection.demo;

        String result = a1+"\n"+a2+"\n"+a3+"\n"+a4+"\n"+a5+"\n"+a6+"\n"+a7+"\n"+a8+"\n"+a9+"\n"+a10+"\n"+a11+"\n"+a12+"\n"+a13+"\n"+a14+"\n"+a15+"\n"+a16+"\n"+a17+"\n"+a18+"\n"+a19;
        tvInfo.setText(result);*/


        tvCpu.setText(" 处理器信息  "+DevInfoUtil.getCpuInfo("Hardware"));
        tvModel.setText(" 主板型号  "+DevInfoUtil.getModel());
        tvSn.setText(" 主板序列号  "+DevInfoUtil.getSN());
        String ramStr = "unKnow";
        JSONObject ramObj = DevInfoUtil.getRAM(ctx);
        try {
            ramStr = ramObj.getString("avail")+"/"+ramObj.getString("total");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        tvRam.setText(" 运行内存（可用/总计）  "+ramStr);
        String romStr = "unKnow";
        JSONObject romObj = DevInfoUtil.getROM(ctx);
        try {
            romStr = romObj.getString("avail")+"/"+romObj.getString("total");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        tvRom.setText(" 存储（可用/总计） "+romStr);
        String screen1 = "unKnow";
        String screen2 = "unKnow";
        JSONArray screenArray = DevInfoUtil.getScreenInfo(ctx);
        try {
            screen1 = screenArray.getString(0);
            screen2 = screenArray.getString(1);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        tvScreen1.setText(" 主屏分辨率  "+screen1);
        tvScreen2.setText(" 副屏分辨率  "+screen2);
        if (screenArray.length()<=1) tvScreen2.setVisibility(View.GONE);
        tvAndroid.setText(" Android版本  "+DevInfoUtil.getOsInfo());
        tvVersion.setText(" 系统版本  "+SystemPropertiesReflection.get("ro.build.display.id"));
        tvSysdate.setText(" 当前系统时间  "+MyUtil.formatDateTime(new Date(System.currentTimeMillis())));
        rollTask("SysDateTime",1);
        long kjsc = SP.getLong("BOOT_TIME", SystemClock.elapsedRealtime());
        kjsc = (kjsc + SystemClock.elapsedRealtime()) / 1000;
        tvKjsc.setText(" 累计开机时长  "+kjsc/60/60+"小时"+kjsc/60%60+"分"+kjsc%60+"秒");
        tvKjcs.setText(" 开机次数  "+SP.getInt("BOOT_COUNT", 1));
        tvLastUpdate.setText(" 最后升级时间  "+MyUtil.transferLongToDate("yyyy-MM-dd HH:mm:ss",SystemPropertiesReflection.getLong("ro.build.date.utc") * 1000));
    }

    private Timer rollTask(String flag,int second){
        if (mTimer != null) mTimer.cancel();
        mTimer = new Timer();
        mTimerTask = new TimerTask() {
            @Override
            public void run() {
//                MyUtil.showLog("rollTask",flag+"@"+new Date());
                if (flag.equals("SysDateTime")) {
                    getActivity().runOnUiThread(new Runnable() {
                        public void run() {
                            tvSysdate.setText(" 当前系统时间  "+MyUtil.formatDateTime(new Date(System.currentTimeMillis())));
                        }
                    });
                }
            }
        };
        mTimer.schedule(mTimerTask,1 * 1000,second * 1000);
        return mTimer;
    }
    private void stopTask(Timer t) {
//        MyUtil.showLog("stopTask", t);
        if (t != null) t.cancel();
    }
}